package com.happy.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.happy.study.entity.NoteEntity;
import com.happy.study.entity.StuNoteEntity;
import com.happy.study.entity.UserEntity;
import com.happy.study.servier.INoteService;
import com.happy.study.servier.IStuNoteService;
import com.happy.study.util.ResultBody;
import com.happy.study.util.StateEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 16:16 2021/1/23
 * @return
 */
@Controller
@RequestMapping ("/note")
public class NoteController {


    @Resource
    private INoteService iNoteService;
    @Resource
    private IStuNoteService iStuNoteService;


    @RequestMapping ("/queryList")
    @ResponseBody
    public ResultBody<NoteEntity> queryList (String noteEntity) {
        NoteEntity note = JSONObject.parseObject ( noteEntity, NoteEntity.class );
        List<NoteEntity> noteEntities = iNoteService.queryList ( note );
        int count = iNoteService.queryCount ( note );
        return ResultBody.success ( noteEntities, count );
    }


    /**
     * 获取是否有权限获取笔记
     * @param id
     * @return
     */
    @GetMapping ("querySingle")
    @ResponseBody
    public ResultBody<NoteEntity> querySingle (int id, HttpSession session) {
        UserEntity userEntity = (UserEntity) session.getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        StuNoteEntity stuNoteEntity = new StuNoteEntity ( userEntity.getId (), id );
        List<StuNoteEntity> stuNoteEntities = iStuNoteService.queryList ( stuNoteEntity );
        if ( stuNoteEntities == null || stuNoteEntities.size () == 0 ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        return ResultBody.success ( StateEnum.SUCCESS );
    }

    /**
     * 获取一个笔记
     * @param id
     * @param model
     * @return
     */
    @GetMapping ("{id}")
    public String loadNote (@PathVariable int id, Model model, HttpSession session) {
        UserEntity userEntity = (UserEntity) session.getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            return "redirect:/note";
        }
        StuNoteEntity stuNoteEntity = new StuNoteEntity ( userEntity.getId (), id );
        List<StuNoteEntity> stuNoteEntities = iStuNoteService.queryList ( stuNoteEntity );
        if ( stuNoteEntities == null || stuNoteEntities.size () == 0 ) {
            return "redirect:/note";
        }
        NoteEntity noteEntity = iNoteService.querySingle ( id );
        model.addAttribute ( "noteEntity", noteEntity );
        return "note";
    }

}
